package com.yaoyong.yy_demo.module.mybatis_plus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaoyong.yy_demo.module.mybatis_plus.entity.DemoUser;
import com.yaoyong.yy_demo.module.mybatis_plus.service.IDemoUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "mybatis_plus演示controller",description = "演示mybatis_plus的基本功能")
@RestController
@RequestMapping("/mybatis_plus")
public class DemoUserController {

    @Autowired
    public IDemoUserService demoUserService;

    @Operation(summary = "添加用户接口",description = "添加用户")
    @PostMapping("save")
    public ResponseEntity<Boolean> save(@RequestBody DemoUser user) {
        try {
//            DemoUser user = new DemoUser();
//            user.setUserName("蒙奇.D.路飞");
//            user.setPassWord("dddddd");
//            user.setSex(1);
            user.setId(null);
            user.setBirthday(LocalDateTime.now());
            user.setCreateTime(LocalDateTime.now());
            boolean save = this.demoUserService.save(user);
            return ResponseEntity.ok(save);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @Operation(summary = "查询用户列表接口",description = "查询用户列表")
    @GetMapping("list")
    public ResponseEntity<List<DemoUser>> list() {
        try {
            List<DemoUser> users = this.demoUserService.list();
            users.forEach(System.out::println);
            return ResponseEntity.ok(users);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @Operation(summary = "根据ID查询用户接口",description = "根据ID查询用户")
    @Parameter(name = "id",description = "用户ID")
    @GetMapping("getById")
    public ResponseEntity<DemoUser> getById(String id) {
        try {
            DemoUser user = this.demoUserService.getById(id);
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }




    @Operation(summary = "统计用户数接口",description = "统计用户数")
    @GetMapping("count")
    public ResponseEntity<Integer> selectCount() {
        try {
            QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
            int count = this.demoUserService.count();
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }




    @Operation(summary = "分页查询用户接口1",description = "分页查询用户返回带分页的用户对象")
    @Parameter(name = "current",description = "当前页")
    @Parameter(name = "size",description = "每页显示的条数")
    @GetMapping("page")
    public ResponseEntity<Page<DemoUser>> page(@RequestParam(name = "current", defaultValue = "0") Long current,
                                               @RequestParam(name = "size", defaultValue = "10") Long size) {
        try {
            QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
            Page<DemoUser> page = new Page<>(0,2);
            Page<DemoUser> userPage = this.demoUserService.page(page,queryWrapper);
            return ResponseEntity.ok(userPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @Operation(summary = "分页查询用户接口2",description = "分页查询用户返回带分页的用户json对象")
    @Parameter(name = "current",description = "当前页")
    @Parameter(name = "size",description = "每页显示的条数")
    @GetMapping("pageMaps")
    public ResponseEntity<Page<Map<String,Object>>> pageMaps(
            @RequestParam(name = "current", defaultValue = "0") Long current,
            @RequestParam(name = "size", defaultValue = "10") Long size) {
        try {
            QueryWrapper<DemoUser> queryWrapper = new QueryWrapper<>();
            Page<Map<String,Object>> page = new Page<>(0,2);
            Page<Map<String,Object>> userPage = this.demoUserService.pageMaps(page, queryWrapper);
            return ResponseEntity.ok(userPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}

